﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class TaiKhoanTietKiemDA
    {

        private static TaiKhoanTietKiem GetTaiKhoanTietKiem(SqlDataReader dataReader)
        {
            TaiKhoanTietKiem tk = new TaiKhoanTietKiem();
            tk.MaTkTietKiem = Convert.ToInt32(dataReader["MaTkTietKiem"]);
            tk.MaTKChinh = Convert.ToInt32(dataReader["MaTKChinh"]);
            tk.SoTien = Convert.ToSingle(dataReader["SoTien"]);
            tk.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
            tk.MaChiTietLS = Convert.ToInt32(dataReader["MaChiTietLS"]);
            tk.NgayGui = Convert.ToDateTime(dataReader["NgayGui"]);
            tk.NgayDaoHan = Convert.ToDateTime(dataReader["NgayDaoHan"]);
            tk.MaLoaiTK = Convert.ToInt32(dataReader["MaLoaiTK"]);
            tk.ChiTietLaiSuat = ChiTietLaiSuatDA.GetByID(tk.MaChiTietLS);
            return tk;
        }
        public static TaiKhoanTietKiem GetByID(int maID)
        {
            TaiKhoanTietKiem tk = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TaiKhoanTietKiem_GetById", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maTKTK", maID));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                tk = GetTaiKhoanTietKiem(dataReader);
            }
            cn.Close();
            return tk;
        }
        public static List<TaiKhoanTietKiem> GetByMaTaiKhoanChinh(int maID,int type)
        {
            List<TaiKhoanTietKiem> list = new List<TaiKhoanTietKiem>();
            TaiKhoanTietKiem tk = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TaiKhoanTietKiem_GetByMaTKChinh", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maTKChinh", maID));
            cmd.Parameters.Add(new SqlParameter("@type", type));
            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                tk = GetTaiKhoanTietKiem(dataReader);
                list.Add(tk);
            }
            cn.Close();
            return list;
        }
        public static bool DaoHan(TaiKhoanTietKiem tk, int type)
        {
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TKTietKiem_UpdateNgay", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@MaTkTietKiem", tk.MaTkTietKiem));
            cmd.Parameters.Add(new SqlParameter("@sotien", tk.SoTien));
            cmd.Parameters.Add(new SqlParameter("@ngaygui", tk.NgayGui));
            cmd.Parameters.Add(new SqlParameter("@ngaydaohan", tk.NgayDaoHan));
            cmd.Parameters.Add(new SqlParameter("@type", type));
            cmd.ExecuteNonQuery();
            cn.Close();

            return true;
        }
        public static bool Insert(TaiKhoanTietKiem tk, int type)
        {
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TKTietKiem_Insert", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@MaTKChinh", tk.MaTKChinh));
            cmd.Parameters.Add(new SqlParameter("@sotien", tk.SoTien));
            cmd.Parameters.Add(new SqlParameter("@MaChiTietLS", tk.MaChiTietLS));
            cmd.Parameters.Add(new SqlParameter("@MaLoaiTK", tk.MaLoaiTK));
            cmd.Parameters.Add(new SqlParameter("@ngaygui", tk.NgayGui));
            cmd.Parameters.Add(new SqlParameter("@ngaydaohan", tk.NgayDaoHan));
            cmd.Parameters.Add(new SqlParameter("@type", type));
            cmd.ExecuteNonQuery();
            cn.Close();

            return true;
        }

        public static bool RutSoTK(TaiKhoanTietKiem tk, int type)
        {
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TKTietKiem_RutSoTK", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@MaTkTietKiem", tk.MaTkTietKiem));
            cmd.Parameters.Add(new SqlParameter("@DelFlg", 1));
            cmd.Parameters.Add(new SqlParameter("@type", type));
            cmd.ExecuteNonQuery();
            cn.Close();

            return true;
        }
    }
}
